package org.spark.java.transformation;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.VoidFunction;

import java.util.Arrays;
import java.util.List;

/**
 * @author: mdf
 * @Date: 2020/1/16 19:35
 * @Description:
 */
public class Map {
    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf();
        //设置运行环境
        sparkConf.setMaster("local");
        sparkConf.setAppName("map");
        JavaSparkContext context = new JavaSparkContext(sparkConf);
        //模拟数据
        List<String> list = Arrays.asList("saber", "hadoop", "spark");
        JavaRDD<String> listRdd = context.parallelize(list);
        JavaRDD<String> map = listRdd.map(new Function<String, String>() {
            public String call(String str) throws Exception {
                //对list的每个值打印hello
                return "hello" + str;
            }
        });
        map.foreach(new VoidFunction<String>() {
            public void call(String str) throws Exception {
                System.out.println(str);
            }
        });
    }
}
